package mht.practice.sort;

/**
 * 冒泡排序法
 * Created by MHT on 2017/8/25.
 */
public class BubbleSort {

    private BubbleSort(){}

    //数组中两个元素进行位置交换
    private static void swap(Object arr[],int i,int j){
        Object temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void sort(Comparable arr[]){
        int n = arr.length;
        boolean swapped = false;

        do {
            swapped = false;
            for (int i=1;i<n;i++){
                if (arr[i-1].compareTo(arr[i]) > 0){
                    swap(arr,i-1,i);
                    swapped = true;
                }
            }
            n--;
        }while (swapped);
    }

    public static void main(String[] args) {

//        //基本数组测试
//        Comparable[] a = {1234,912,844,753,623,510,455,326,210,110};
//        SelectionSort.sort(a);
//        for (int i=0;i<a.length;i++){
//            System.out.print(a[i]+"  ");
//        }
//        System.out.println();


        int N = 20000;
        Integer[] b = SortTestHelper.generateRandomArray(N,0,100000);
        SortTestHelper.testSort("mht.practice.sort.BubbleSort",b);

    }
}
